package com.yanqu.road.server.manger.player.task;

import com.yanqu.road.entity.enums.eRedHotNotifyType;
import com.yanqu.road.entity.tradewar.UserTradeWarInfo;
import com.yanqu.road.entity.tradewar.UserTradeWarRecord;
import com.yanqu.road.logic.bussiness.player.UserTradeWarBussiness;
import com.yanqu.road.pb.worldmessage.WorldMessageProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.TradeWarModule;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.pb.WorldMessagePb;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.executor.GeneralTask;

public class AddTradeWarRecordTask extends GeneralTask {

    private UserTradeWarRecord tradeWarRecord;

    public AddTradeWarRecordTask(UserTradeWarRecord tradeWarRecord) {
        this.tradeWarRecord = tradeWarRecord;
    }

    @Override
    public void doRun() {
        GamePlayer player = GamePlayerMgr.getOnlinePlayer(tradeWarRecord.getUserId());
        if (null != player) {
            player.getModule(TradeWarModule.class).addRecord(tradeWarRecord);
            // 通知来访红点
            WorldMessageProto.RedHotNotifyMsg.Builder redHotMsg = WorldMessagePb.parseRedHotNotifyMsg(eRedHotNotifyType.TradeWarBeAttack.getValue(), 1, "");
            player.sendPacket(Protocol.U_RED_HOT_NOTIFY, redHotMsg);
            UserTradeWarInfo tradeWarInfo = player.getModule(TradeWarModule.class).getUserTradeWarInfo();
            if(null != tradeWarInfo) {
                tradeWarInfo.setLastBeAttackNotifyTime(tradeWarRecord.getRecordTime());
            }
        } else {
            UserTradeWarBussiness.addUserTradeWarRecord(tradeWarRecord);
        }
    }
}
